<template>
  <div>
    <kendoForm :kendo-form-items="kendoFormItems" :kendo-form="kendoForm" :kendo-params-value="kendoParamsValue" />
  </div>
</template>

<script>
  import kendoForm from '@/Views/components/kendo-vue-component/kendoForm/index.vue'
  import { isBlank, isEmpty } from '@/Utils/strUtil.js'
  export default {
    name: 'DictOperate',
    components: {
      kendoForm
    },
    data() {
      return {
        dialogName: this.$attrs.data.dialogName,
        kendoFormItems: [
          {
            name: 'reqName',
            label: '需求名称',
            paramName: 'reqName',
            editDisabled: true,
            viewDisabled: true,
            required: true,
            controlType: 'input'
          },
          {
            ref: 'REQ_CLASSIFY_CD',
            remoteData: true,
            cascade: true,
            name: 'reqClassify',
            label: '需求分类',
            paramName: 'reqClassify',
            optionLabel: '--请选择--',
            controlType: 'dropDownList',
            filter: 'contains',
            dataType: 'json',
            transportReadUrl: '/service/sysMng/dict/findDictByTypeWithForm',
            required: true,
            autoBind: true,
            // editDisabled: true,
            viewDisabled: true,
            schema: {
              data: function(response) {
                return response.data
              }
            },
            dataMapping: {
              label: 'dictValNm',
              value: 'dictVal'
            },
            parameterMap: function(options, operation) {
              if (operation !== 'read' && options.models) {
                // eslint-disable-next-line no-undef
                return { models: kendo.stringify(options.models) }
              } else {
                // eslint-disable-next-line no-undef
                return kendo.stringify({ dict: 'REQ_CLASSIFY_CD' })
              }
            }
          },
          {
            name: 'reqReceiveDt',
            label: '接收日期',
            paramName: 'reqReceiveDt',
            required: true,
            editDisabled: false,
            viewDisabled: true,
            controlType: 'datePicker',
            format: 'yyyy-MM-dd'
          },
          {
            name: 'devInCharge',
            label: '开发负责人',
            paramName: 'devInCharge',
            required: true,
            viewDisabled: true,
            controlType: 'multiselect',
            // change: this.devInChargeChange,
            dataSource: 'PrjMemberDataSource',
            dataType: 'json',
            transportReadUrl: '/service/prjMng/prjMemberMng/findPrjMemberList',
            dataMapping: {
              label: 'memberName',
              value: 'memberNo'
            },
            autoBind: true,
            remoteData: true,
            schema: {
              data: function(response) {
                if (typeof response.data === 'object') {
                  if (JSON.stringify(response.data) === '{}') {
                    return []
                  }
                }
                return response.data
              }
            }
          },
          {
            name: 'reqInCharge',
            label: '需求负责人',
            paramName: 'reqInCharge',
            required: true,
            editDisabled: true,
            viewDisabled: true,
            controlType: 'input'
          },
          {
            name: 'reqTaskExecutor',
            label: '需求任务执行人',
            paramName: 'reqTaskExecutor',
            required: true,
            viewDisabled: true,
            controlType: 'input'
          },
          {
            name: 'estimatedWorkload',
            label: '预估工作量(人/月)',
            paramName: 'estimatedWorkload',
            controlType: 'numeric',
            required: true,
            viewDisabled: true,
            change: this.estimatedWorkloadChange
          },
          {
            label: '批复工作量(人/月)',
            name: 'apprWorkload',
            paramName: 'apprWorkload',
            controlType: 'numeric',
            viewDisabled: true,
            change: this.apprWorkloadChange
          },
          {
            name: 'scheduledStartDt',
            label: '计划开始日期',
            paramName: 'scheduledStartDt',
            controlType: 'datePicker',
            format: 'yyyy-MM-dd',
            disableDates: ['sa', 'su'],
            required: true,
            // editDisabled: true,
            viewDisabled: true,
            change: this.scheduledStartDtChange
          },
          {
            name: 'scheduledEndDt',
            label: '计划开始结束日期',
            paramName: 'scheduledEndDt',
            controlType: 'datePicker',
            format: 'yyyy-MM-dd',
            required: true,
            readonly: true,
            // editDisabled: true,
            viewDisabled: true
          },

          {
            name: 'actualStartDt',
            label: '实际开始日期',
            paramName: 'actualStartDt',
            controlType: 'datePicker',
            disableDates: ['sa', 'su'],
            format: 'yyyy-MM-dd',
            viewDisabled: true
          },
          {
            name: 'actualEndDt',
            label: '实际结束日期',
            paramName: 'actualEndDt',
            controlType: 'datePicker',
            disableDates: ['sa', 'su'],
            format: 'yyyy-MM-dd',
            viewDisabled: true
          },
          {
            ref: 'WHETHER_DELAY_CD',
            remoteData: true,
            cascade: true,
            name: 'whetherDelay',
            label: '是否逾期',
            paramName: 'whetherDelay',
            optionLabel: '--请选择--',
            controlType: 'dropDownList',
            filter: 'contains',
            dataType: 'json',
            transportReadUrl: '/service/sysMng/dict/findDictByTypeWithForm',
            required: true,
            autoBind: true,
            editDisabled: false,
            viewDisabled: true,
            schema: {
              data: function(response) {
                return response.data
              }
            },
            dataMapping: {
              label: 'dictValNm',
              value: 'dictVal'
            },
            parameterMap: function(options, operation) {
              if (operation !== 'read' && options.models) {
                // eslint-disable-next-line no-undef
                return { models: kendo.stringify(options.models) }
              } else {
                // eslint-disable-next-line no-undef
                return kendo.stringify({ dict: 'WHETHER_DELAY_CD' })
              }
            }
          },
          {
            ref: 'CURRENT_GENERATION_CD',
            remoteData: true,
            cascade: true,
            name: 'currentGeneration',
            label: '当前阶段',
            paramName: 'currentGeneration',
            optionLabel: '--请选择--',
            controlType: 'dropDownList',
            filter: 'contains',
            dataType: 'json',
            transportReadUrl: '/service/sysMng/dict/findDictByTypeWithForm',
            required: true,
            autoBind: true,
            viewDisabled: true,
            schema: {
              data: function(response) {
                return response.data
              }
            },
            dataMapping: {
              label: 'dictValNm',
              value: 'dictVal'
            },
            parameterMap: function(options, operation) {
              if (operation !== 'read' && options.models) {
                // eslint-disable-next-line no-undef
                return { models: kendo.stringify(options.models) }
              } else {
                // eslint-disable-next-line no-undef
                return kendo.stringify({ dict: 'CURRENT_GENERATION_CD' })
              }
            }
          },
          {
            label: '总体进度',
            name: 'overallProgress',
            paramName: 'overallProgress',
            required: true,
            viewDisabled: true,
            controlType: 'numeric',
            format: 'p2',
            min: 0,
            max: 1,
            step: 0.01
          },
          {
            genre: 'special',
            label: '备注',
            name: 'remarks',
            paramName: 'remarks',
            controlType: 'textarea'
            // format: '￥0'
          }
        ],
        kendoForm: {
          ref: 'ReqmntTaskForm',
          valid: 'valid',
          buttons: [
            {
              name: '保存', // 按钮内容
              class: 'k-primary', // 按钮样式
              icon: 'save',
              click: this.save
            },
            {
              name: '重置', // 按钮内容
              icon: 'reset-sm',
              click: this.reset
            }
          ],
          rules: {},
          message: {}
        },
        kendoParamsValue: {
          id: '',
          reqNo: '',
          reqName: '',
          reqClassify: '',
          reqReceiveDt: '',
          devInCharge: '',
          reqInCharge: '',
          reqTaskExecutor: '',
          scheduledStartDt: '',
          scheduledEndDt: '',
          actualStartDt: '',
          actualEndDt: '',
          whetherDelay: '',
          estimatedWorkload: '',
          apprWorkload: '',
          currentGeneration: '',
          overallProgress: '',
          createTime: '',
          createOrg: '',
          createRole: '',
          createUser: '',
          updateTime: '',
          updateOrg: '',
          updateRole: '',
          updateUser: '',
          hdlTime: '',
          hdlOrg: '',
          hdlRole: '',
          hdlUser: ''
        }
      }
    },
    mounted() {
      this.kendoParamsValue = Object.assign(this.kendoParamsValue, this.$attrs.data.param)
      const operatorType = this.$attrs.data.operatorType
      if (operatorType) {
        if (operatorType === 'edit') {
          this.kendoFormItems.forEach(item => {
            // Array.isArray(this.kendoParamsValue[item.paramName])
            if (!isEmpty(this.kendoParamsValue[item.paramName]) &&
              typeof this.kendoParamsValue[item.paramName] === 'string' &&
              this.kendoParamsValue[item.paramName].split(',').length > 1) {
              this.$set(this.kendoParamsValue, item.paramName, this.kendoParamsValue[item.paramName].split(','))
            }
            if (item.editDisabled) {
              item['readonly'] = true
            }
          })
          this.kendoForm = {
            ref: 'ReqmntTaskForm',
            valid: 'valid',
            buttons: [
              {
                name: '保存', // 按钮内容
                class: 'k-primary', // 按钮样式
                icon: 'save',
                click: this.save
              }
            ]
          }
        } else if (operatorType === 'view') {
          this.kendoFormItems.forEach(item => {
            if (!isEmpty(this.kendoParamsValue[item.paramName]) &&
              typeof this.kendoParamsValue[item.paramName] === 'string' &&
              this.kendoParamsValue[item.paramName].split(',').length > 1) {
              this.$set(this.kendoParamsValue, item.paramName, this.kendoParamsValue[item.paramName].split(','))
            }
            if (item.viewDisabled) {
              item['disabled'] = true
            }
          })
        }
      }
    },
    methods: {
      save(paramVal, validator) {
        if (validator.validate()) {
          this.kendoFormItems.forEach(item => {
            if (Array.isArray(paramVal[item.paramName])) {
              this.$set(paramVal, item.paramName, paramVal[item.paramName].join(','))
            }
            if (item.viewDisabled) {
              item['disabled'] = true
            }
          })
          this.$post('/service/prjMng/reqmntTaskMng/saveReqMntTask', paramVal).then(res => {
            if (res.code !== 200) {
              this.$KendoNotify({
                title: '错误信息:' + res.code,
                message: res.message
              }, 'error')
            } else {
              this.$KendoNotify({
                title: '提示信息:',
                message: res.message
              }, 'success')
              this.$bus.emit('dialog' + `${this.dialogName}`, true)
            }
          })
        }
      },
      reset(paramVal) {
        Object.keys(paramVal).forEach(key => {
          if (key !== 'reqNo') {
            if (Array.isArray(paramVal[key])) {
              this.kendoParamsValue[key] = []
            } else {
              this.kendoParamsValue[key] = ''
            }
          }
        })
      },
      devInChargeChange(e) {
        const devInCharge = e.sender.value()
        this.$post('/service/prjMng/reqmntTaskMng/getMinDateForScheduled', { devInCharge: devInCharge }).then(res => {
          if (res && res.code === 200) {
            const date = res.data
            const formItem = this.kendoFormItems.filter(s => s.paramName === 'scheduledStartDt')
            let scheduledStartDtItem
            if (formItem && formItem.length > 0) {
              scheduledStartDtItem = formItem[0]
            }
            const index = this.kendoFormItems.map(s => s.paramName).indexOf('scheduledStartDt')
            if (index >= 0) {
              this.kendoFormItems.splice(index, 1)
              this.$set(scheduledStartDtItem, 'minDate', new Date(date))
              this.kendoFormItems.splice(index, 0, scheduledStartDtItem)
            }
          } else {
            this.$KendoNotify({
              title: '错误信息:' + res.code,
              message: res.message
            }, 'error')
          }
        })
      },
      // 计划开始日期+工作量 计算计划结束时间
      scheduledStartDtChange(e) {
        // dateAddDays
        const date = e.sender.value()
        // 判断计划开始日期不能大于计划结束日期
        if (isBlank(this.kendoParamsValue.apprWorkload) && isBlank(this.kendoParamsValue.estimatedWorkload)) {
          this.$KendoNotify({
            title: '警告信息:',
            message: '请输入预估工作量或者批复工作量！'
          }, 'warning')
          this.kendoParamsValue.scheduledStartDt = ''
        } else {
          if (isBlank(this.kendoParamsValue.apprWorkload)) {
            const params = {
              // eslint-disable-next-line no-undef
              scheduledStartDt: kendo.toString(kendo.parseDate(date, 'yyyy-MM-dd'), 'yyyy-MM-dd'),
              workload: this.kendoParamsValue.estimatedWorkload
            }
            this.$post('/service/prjMng/reqmntTaskMng/addDaysSkipHoliday', params).then(res => {
              if (res && res.code === 200) {
                this.kendoParamsValue.scheduledEndDt = res.data
              } else {
                this.$KendoNotify({
                  title: '错误信息:' + res.code,
                  message: res.message
                }, 'error')
              }
            })
          } else {
            const params = {
              // eslint-disable-next-line no-undef
              scheduledStartDt: kendo.toString(kendo.parseDate(date, 'yyyy-MM-dd'), 'yyyy-MM-dd'),
              workload: this.kendoParamsValue.apprWorkload
            }
            this.$post('/service/prjMng/reqmntTaskMng/addDaysSkipHoliday', params).then(res => {
              if (res && res.code === 200) {
                this.kendoParamsValue.scheduledEndDt = res.data
              } else {
                this.$KendoNotify({
                  title: '错误信息:' + res.code,
                  message: res.message
                }, 'error')
              }
            })
          }
        }
      },
      estimatedWorkloadChange(e) {
        const val = e.sender.value()
        const date = this.kendoParamsValue.scheduledStartDt
        // eslint-disable-next-line no-undef
        if (isBlank(kendo.toString(kendo.parseDate(date, 'yyyy-MM-dd'), 'yyyy-MM-dd'))) {
          return
        } else {
          if (isBlank(this.kendoParamsValue.apprWorkload)) {
            const params = {
              // eslint-disable-next-line no-undef
              scheduledStartDt: kendo.toString(kendo.parseDate(date, 'yyyy-MM-dd'), 'yyyy-MM-dd'),
              workload: val
            }
            this.$post('/service/prjMng/reqmntTaskMng/addDaysSkipHoliday', params).then(res => {
              if (res && res.code === 200) {
                this.kendoParamsValue.scheduledEndDt = res.data
              } else {
                this.$KendoNotify({
                  title: '错误信息:' + res.code,
                  message: res.message
                }, 'error')
              }
            })
          } else {
            const params = {
              // eslint-disable-next-line no-undef
              scheduledStartDt: kendo.toString(kendo.parseDate(date, 'yyyy-MM-dd'), 'yyyy-MM-dd'),
              workload: this.kendoParamsValue.apprWorkload
            }
            this.$post('/service/prjMng/reqmntTaskMng/addDaysSkipHoliday', params).then(res => {
              if (res && res.code === 200) {
                this.kendoParamsValue.scheduledEndDt = res.data
              } else {
                this.$KendoNotify({
                  title: '错误信息:' + res.code,
                  message: res.message
                }, 'error')
              }
            })
          }
        }
      },
      apprWorkloadChange(e) {
        const val = e.sender.value()
        const date = this.kendoParamsValue.scheduledStartDt
        // eslint-disable-next-line no-undef
        if (isBlank(kendo.toString(kendo.parseDate(date, 'yyyy-MM-dd'), 'yyyy-MM-dd'))) {
          return
        } else {
          const params = {
            // eslint-disable-next-line no-undef
            scheduledStartDt: kendo.toString(kendo.parseDate(date, 'yyyy-MM-dd'), 'yyyy-MM-dd'),
            workload: val
          }
          this.$post('/service/prjMng/reqmntTaskMng/addDaysSkipHoliday', params).then(res => {
            if (res && res.code === 200) {
              this.kendoParamsValue.scheduledEndDt = res.data
            } else {
              this.$KendoNotify({
                title: '错误信息:' + res.code,
                message: res.message
              }, 'error')
            }
          })
        }
      }
    }
  }
</script>

<style>

</style>
